﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Models;
using System.IO;

namespace ChoDiaOc_HK_WPF.Views.KhachHang {
    /// <summary>
    /// Interaction logic for DanhSachDiaOcDaDangKy.xaml
    /// </summary>
    public partial class DanhSachDiaOcDaDangKy : UserControl {
        private DBEntities entities = new DBEntities();
        public DanhSachDiaOcDaDangKy() {
            InitializeComponent();
            entities = new DBEntities();
        }

        private NOIDUNGQC noiDungQC;
        public ChoDiaOc_HK_WPF.NOIDUNGQC NoiDungQC {
            get {
                if (noiDungQC == null) {
                    noiDungQC = new NOIDUNGQC();
                }
                return noiDungQC;
            }
            set { noiDungQC = value; }
        }

        private void pHIEUDANGKiesListView_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (pHIEUDANGKiesListView.SelectedIndex != -1) {
                imgAnhChup.Source = null;
                DIAOC diaOc = (pHIEUDANGKiesListView.SelectedItem as PHIEUDANGKY).DIAOC;
                PHIEUDANGKY phieu = pHIEUDANGKiesListView.SelectedItem as PHIEUDANGKY;
                QC_BAO qcBao = phieu.QC_BAO.FirstOrDefault();
                QC_BANG qcBang = phieu.QC_BANG.FirstOrDefault();
                TOBUOM qcBuom = phieu.TOBUOMs.FirstOrDefault();
                NOIDUNGQC noiDung = null;
                if (qcBang != null) {
                    noiDung = qcBang.NOIDUNGQC;
                } else {
                    if (qcBao != null) {
                        noiDung = qcBao.NOIDUNGQC;
                    } else {
                        if (qcBuom != null) {
                            noiDung = qcBuom.NOIDUNGQC;
                        }
                    }
                }
                if (noiDung != null) {
                    NoiDungQC.MaND = noiDung.MaND;
                }

                System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["hINHANHsViewSource"];
                myCollectionViewSource.Source = entities.HINHANHs.Where(ha => ha.MaND == NoiDungQC.MaND);
            }
        }

        private void hINHANHsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (hINHANHsListView.SelectedIndex != -1) {
                HINHANH ha = hINHANHsListView.SelectedItem as HINHANH;
                FileStream stream = new FileStream("./AnhChupDiaOc/" + ha.Anh, FileMode.Open, FileAccess.Read);
                Image i = new Image();
                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.StreamSource = stream;
                src.EndInit();
                imgAnhChup.Source = src;
                imgAnhChup.Stretch = Stretch.Fill;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            CollectionViewSource collectionViewSource = (CollectionViewSource)FindResource("phieuDangKyListSource");
            String maKH = DatabaseWrapper.GetInstance().PassWord;
            collectionViewSource.Source = entities.PHIEUDANGKies.Where(t => t.KHACHHANG.MaKH == maKH);
        }
    }
}
